MYOWD=/data/data/com.termux/files/home/gitee/emp
MYOWD=/data/data/com.termux/files/home/gitee/emp
if [[ -z $MYOWD ]];then
	MYOWD='.'
fi
source $MYOWD/base.sh
source $MYOWD/163.sh
A='🍎'
get_login="jq .profile|{uname:.nickname,uid:.userId}"
login_file="$Base163Dir/.163/jq.login"
nget(){
	case $1 in
		api)$get_login |jq $2 ;;
		uid|uname)jq .$1 $login_file ;;
		lists)api list lists $2 |jq -r ".playlist|.[]|$3" ;;
		songs)if [[ $2 = search ]];then 
			api search song "$3"|jq -r ".result.songs|.[]|$4"
		else
			api list songs $2 |jq -r ".songs|.[]|$4" 
		fi ;;
	esac
}
getline(){
	case $# in
		1)sed -n "$1 p" ;;
		2)sed -n "$1,$2 p" ;;
	esac
}
login_check(){  #检查登陆状态，及时重置
	uid=$(api login $1 $2 $3 |nget api .uid)
	if [[ ! $uid =~ [0-9]+ ]];then
		echo '账号或密码错啦. '
		api refresh login $1 $2 $3
		exit
	fi
	api login $1 $2 $3 |$get_login > $login_file
}
check(){
	case $1 in
		phone|captcha)
			if ! [[  $2 =~ ^1[35789][0-9]{9} ]];then
				echo '手机 号码 输错 啦。🍎'
				exit
			fi ;;
		email)
			#^[a-zA-Z0-9_-]+@[a-z]+(\.[a-z]+)+$
			if ! [[ $2 =~ ^[a-zA-Z0-9_-]+@[a-z]+(\.[a-z]+)+$ ]];then
				echo '邮箱 号码 输错 啦。🍎'
				exit
			fi ;;
		num)
			if ! [[ $2 =~ [0-9]+ && $2 -gt 0 && $2 -lt $3 ]];then
				echo '输入不对啦。🍎'
			else
				isnum=yes
			fi
			;;
	esac
}
login(){
	case $1 in
		p|phone)
			read -p '手机号码 ：' n 
			case $2 in
				p|password)
					m=phone
					read -p '密码：' p ;;
				c|captcha)m=captcha ;;
				*)input_error ;;
			esac ;;
		e|email)
			m=email
			read -p '邮箱账号：' n 
			read -p '邮箱密码：' p  ;;
		*)
			echo '''
		login phone/p password/p  #手机密码 登陆
		login phone/p captcha/c  #手机短信 登陆
		login email/e  #邮箱 登陆
		'''
			input_error ;;
	esac

	check $m "$n"
	if [[ $m = 'captcha' ]];then
		api login send "$n"
		read -p '验证码: ' c
		login_check $m $n $c
	else
		login_check $m $n $p
	fi
	echo "欢迎用户: $(nget uname) 🍎"
}
choose_pid(){
	uid=$(nget uid)
	echo "你的歌单  🍎"
	nget lists $uid  '.name'|grep -n '.*'
	let t=$(nget lists $uid  '.name'|sed -n $=)+1
	read -p '输入：' n
	check num $n $t
	pid=$(nget lists $uid  ".id" |sed -n "$n p")
	pname=$(nget lists $uid  ".name" |sed -n "$n p")
	pname=$(echo $pname|sed 's# #_#g; s#(#_#g; s#)#_#g; s#，#_#g; s#\[#_#g; s#\]#_#g;')
}
search_pid(){  #choose a pid from search result,, the pid is used for gernarate a song list 
	#if song, then no needed, because itself is a list, then return 
	
	name="genshi"
	read -p 'input: ' name
	case $1 in 
		song) 
			api search song "$name" |jq '.result.songs|.[].name'|grep -n '.*' ;;
		list)
			api search list "$name" |jq '.result.playlists|.[]|"\(.name),  \(.description)"'|grep -n '.*' ;
			read -p 'input: ' n
			check num $n 31 
			if [[ $isnum = 'yes' ]] ;then

				pid=$(api search list "$name" |jq ".result.playlists|.[].id" |sed -n "$n p") ;
				pname=$(api search list "$name" |jq -r ".result.playlists|.[].name" |sed -n "$n p") ;
				pname=$(echo $pname|sed 's# #_#g; s#(#_#g; s#)#_#g; s#，#_#g; s#[#_#g; s#]#_#g;')
			else
				n='default'
			fi ;;
	esac
}
play(){

		let t=$(sed -n $= $TmpSongs)+1
		check num $n $t
		if [[ $isnum = 'yes' ]];then 

			sid=$(sed -n "$n p" $TmpSongs|sed 's/.*id://')
			info=$(sed -n "$n p" $TmpSongs)

			api geturl song $sid  |jq ".data|.[].url"
			url=$(api geturl song $sid  |jq -r ".data|.[].url")
			echo "$A $info"
			$MYOWD/client.sh toggle 
			$Player_Cmd "$url"
			$MYOWD/client.sh toggle

		fi
}
#api  refresh 
#login_check phone 17382100273 wyy@351393328
#curl "$Baseurl/song/url/v1?id=405998841&level=lossless"
#echo $Baseurl
#api login status
#api login refresh
check_list(){
	list=$(awk '/- name: /{print $3}' $Base163Dir/config.md)
	echo "你的列表：$list  $A"
	for l in $list;do
		if [[ $l = "$1" ]];then
			echo "已经有此名字啦$l $A"
			echo "运行 cc delete $l 删除列表"
			exit
		fi
	done
}

create_net(){
	if [[ -z "$1" ]];then
		echo "请输入名字啦 $A"
		exit
	fi
	check_list "$1"
	case $2 in
		choose_pid)$2 ;;
	esac	
	nget songs $pid 'name' '.name'

	echo "一切就绪 $A"
	echo '''

''' >> $Base163Dir/config.md
	sed -n '/^- name: default/,/^$/ p' $Base163Dir/.config.md|
		sed "s/- name:.*/- name: $1  163网易/" >> $Base163Dir/config.md
	nget songs $pid 'name' '"id:\(.id),mv:\(.mv),filename:\(.name),type:net"'|sed 's#"##g' > $Base163Dir/lists/"$1"
}
#create_net i
#exit
main(){
	case $1 in
		source);;
		login)$1 $2 $3 ;;
		create_net)$1 $2 ;;
		P)$1 ;;
		*)echo '''
		login username passwd 登陆
		create_net name 创建网络歌单
		P 交互式使用
		''' ;;
	esac
}
switch_user(){
	info=$(sed -n '/uname/{s/"uname"://;p}' ${login_file}all|grep -n '.*')
	echo "$info"
	user_num=$(echo "$info"|sed -n $=)
	read -n1 -p 'input: ' n
	(( n>0 && n<= user_num )) &&
		sed -n "$((-3+n*4)),+3 p" ${login_file}all > ${login_file} || echo 'input error!'
}
#switch_user
net_help(){
		echo '''
		$MYOWD/client.sh/c invo...  like,SL(switch list) 

		num  play a song use Player_Cmd(mpv or play)
		login #use phonenumbe login
		import #import other playlist by id
		l|lists for list your lists
		s|songs for quick control you net playlist
		sw for switch user who have logined
		ss for search song
		sl for search list
		add for add current playlist into local player
		q for quit
		'''
}
import_playlist(){
	echo '''name? anything as your like. and uid?  
	goto homepage, click share by links,
	copy links to some where, `id=xxxxx` in links 
	`xxxxx` is your uid '''
	read -p "name: " name
	read -p "uid: " uid
	echo '{' > ${login_file}
	echo \"uname\": \"$name\", >> ${login_file}
	echo \"uid\": $uid >> ${login_file}
	echo "}" >> ${login_file}
	cat ${login_file} >> ${login_file}all
	echo "done !"
}
#import_playlist
	
P(){
	n=net
	TmpSongs="$MYOWD/.163/.tmpsongs"
	while true;do
		[[ $n != default ]] && cat $TmpSongs
		n=net
		net_help
		read -e -p '输入：' n 
		case $n in 
			c*|ccc*)clear;
				bash $MYOWD/client.sh ${n#* } 
				exit
				sleep 5;echo 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh' 
				n=default;;
			Re|close|pre|next|like)$MYOWD/client.sh $n; n=default;;
			SL)$MYOWD/client.sh switch list; n=default;;
			s)$MYOWD/client.sh songs; n=default;;
			login)$MYOWD/net.sh login p c;; 
			import)import_playlist;; 
			l)choose_pid 
				nget songs $pid  "name" '"filename:\(.name),id:\(.id)"'|grep -n '.*' > $TmpSongs
				;;
			add)
				n=default
				if [[ -n $pid ]] ;then
					create_net "$pname"
				fi 
				bash $MYOWD/client.sh switch list "$pname";exit
				sleep 5;echo 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh' 
				#exec $MYOWD/client.sh P
				;;
			sw) switch_user;;
			sl|ss)
				
				[[ $n = sl ]] && search_pid list 
				[[ $n = ss ]] && search_pid song &&pid=search
				# sl:  songs 342423 null
				# ss:  songs search yuanshen ,pid=search
				nget songs $pid "$name" '"filename:\(.name),id:\(.id)"'|grep -n '.*' > $TmpSongs ;;
			q|quit) exit ;;
			[0-9]*)
				play ;;
			*)net_help ;;
		esac
	done
}
#choose_pid
#play
#P
main $@
